//
//  BookStackCategoryRightTableView.m
//  ProjectDemo
//
//  Created by IOS3 on 2018/11/2.
//  Copyright © 2018 IOS3. All rights reserved.
//

#import "BookStackCategoryRightTableView.h"


@interface BookStackCategoryRightTableView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) NSInteger visibleCellTopSection;

@end

@implementation BookStackCategoryRightTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self initializes];
    }
    return self;
}

- (void)initializes {
    self.delegate = self;
    self.dataSource = self;
    [self registerClass:UITableViewCell.class forCellReuseIdentifier:@"BookStackCategoryRightTableViewCell"];
}



#pragma mark - delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BookStackCategoryRightTableViewCell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld - %ld", indexPath.section, indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectIndexPathAt) {
        self.selectIndexPathAt(indexPath);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self tableViewShouldGetTopCellSection];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self tableViewShouldGetTopCellSection];
}
- (void)tableViewShouldGetTopCellSection {
    UITableViewCell *cell = self.visibleCells.firstObject;
    NSIndexPath *indexPath = [self indexPathForCell:cell];
    if (indexPath.section != self.visibleCellTopSection) {
        self.visibleCellTopSection = indexPath.section;
    }
}


- (void)setVisibleCellTopSection:(NSInteger)visibleCellTopSection {
    _visibleCellTopSection = visibleCellTopSection;
    if (self.visibleTopSectionAt) {
        self.visibleTopSectionAt(visibleCellTopSection);
    }
}

@end
